<section class="content-header">
    <!-- <h1>
        {{ $header or trans('admin::lang.title') }}
        <small>{{ $description or trans('admin::lang.description') }}</small>
    </h1> -->
</section>

<section class="content">
    {{-- tian add flash提示信息 --}}
    @include('flash::message')

    @include('admin::partials.error')
    @include('admin::partials.success')
    @include('admin::partials.exception')
    @include('admin::partials.toastr')
    


    <div class="box">
        <!--<div class="box-header">
            <h3 class="box-title"></h3>
            <div class="pull-right">
            </div>
            <span>
            </span>
        </div>-->
        <!-- /.box-header -->
        <div class="box-body table-responsive no-padding">
            <input type="hidden" name="_token" value="{{ csrf_token() }}" />
            <input type="hidden" id="tag_id" value="{{ $tag_id }}" />

            <!-- table start -->
            <div class="row col-lg-12">
                <div class="animated fadeInUp">
                    <div class="ibox">
                        <div class="ibox-title" style="color: inherit;margin-bottom: 0;padding: 0px 15px 7px;min-height: 48px;">
                            <h5>
                                <!-- 所有项目 -->
                            </h5>
                            <div class="ibox-tools rboor" style="background-color: #ffffff;color: inherit;padding: 15px 20px 20px 20px;border-color: #e7eaec;border-image: none;border-style: solid solid none;border-width: 1px 0px;">
                                <a class="btn btn-default btn-sm" id="back-subcolumn-asset" href="javascript:void(0);" onclick="history.go(-1)">
                                    <i class="fa fa-arrow-left">
                                    </i>
                                    返回
                                </a>

                                <a class="btn btn-success btn-sm add-subcolumn-asset" href="javascript:void(0);" tag_id="{{ $tag_id }}">
                                    <i class="im-plus">
                                    </i>
                                   贴标签
                                </a>

                                <a class="btn btn-danger btn-sm" id="batch-delete-subcolumn-asset" href="javascript:void(0);">
                                    <i class="im-remove4">
                                    </i>
                                    批量删除
                                </a>
                                <button class="btn btn-success btn-sm" href="javascript:void(0);" id="subcolumn-asset-selected-refresh">
                                    <i class="im-spinner10 fa-spin">
                                    </i>
                                    刷新
                                </button>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <!-- search start -->
                            <div class="form-horizontal clearfix">


                                    <div class="col-lg-3 col-sm-2 pl0">
                                        <div class="form-group">
                                            <label class="col-lg-4 col-sm-3 control-label">
                                                收费类型：
                                            </label>
                                            <div class="col-lg-8 col-sm-7">
                                                <select class="input-sm form-control input-s-sm inline" id="subcolumn-asset-selected-charge-type">
                                                    <option value="">
                                                        请选择
                                                    </option>
                                                        <option value="0">免费</option>
                                                        <option value="1">收费</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-3 col-sm-3">
                                    <div class="form-group">
                                        <div class="col-lg-12 col-sm-12 input-group">
                                            <input class="input-sm form-control" placeholder="请输入关键字" type="text" id="subcolumn-asset-selected-search-value">
                                                <span class="input-group-btn">
                                                    <button class="btn btn-sm btn-primary" type="button" id="subcolumn-asset-selected-search">
                                                        搜索
                                                    </button>
                                                </span>
                                            </input>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- search end -->
                            <!-- 表格-已选的媒资数据 -->
                            <table cellspacing="0" class="table table-striped table-bordered table-hover dataTables-example dataTable" id="subcolumn-asset-selected-table" width="100%">
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <!-- table end -->
        </div>
    </div>


</section>
<script>

    function entityTagContentAsset() {
        // 防止csrf
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('input[name="_token"]').val()
                // 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        /**
         * 专辑选中的媒资
         */
        // 已选的媒资数据-datatables渲染
        subcolumnAssetSelectedTable = $('#subcolumn-asset-selected-table').DataTable({
            // "processing": true,
            // 服务器端渲染
            "serverSide": true,
            "retrieve": true,
            // 请求数据的形式(get, post), 请求的路径
            ajax: {
                url: "/admin/entity/tag_content/selectedContent",
                type: "POST",
                data: function(d) {
                    // 归类
                    d.vcat = $('#subcolumn-asset-selected-vcat').val();
                    // 分类
                    d.vcattag = $('#subcolumn-asset-selected-vcattag').val();
                    // 收费类型
                    d.charge_type = $('#subcolumn-asset-selected-charge-type').val();
                    // 搜索框
                    d.search_value = $('#subcolumn-asset-selected-search-value').val();
                    // 二级栏目id
                    d.tag_id = $('#tag_id').val();
                }
            },
            columns: [{
                orderable: false,
                data: "id",
                render: function(id) {
                    return '<input type="checkbox" data-id="' + id + '" name="checkbox-subcolumn-asset-selected">'
                },
                title: '<input id="subcolumn-asset-selected-check-all" name="" type="checkbox" value=""/>全选/反选',
                width: "6%"
            }, {
                // 列名
                title: "媒资ID",
                // 数据
                data: "content.id",
                // 能否进行排序
                orderable: false,
                width: "5%"
            }, {
                // 列名
                title: "媒资类型",
                // 数据
                data: "tag_content_type",
                // 能否进行排序
                orderable: false,
                width: "5%"
            }, {
                // 列名
                title: "媒资编号",
                // 数据
                data: "content.content_no",
                // 能否进行排序
                orderable: false,
                width: "5%"
            }, {
                title: "名称",
                data: "content.name",
                orderable: false,
                width: "10%"
            }, {
                title: "标题",
                data: "content.title",
                orderable: false,
                width: "20%"
            }, {
                title: "服务商",
                data: "content.sps",
                orderable: false,
                width: "5%",
                render: function(sps) {
                    if (typeof(sps)=="undefined" || sps.length < 1){
                        return '';
                    }else{
                        var aCats = [];
                        $.each(sps, function(key, sp) {
                            aCats.push(sp['name']);
                        });
                        return aCats.join(',');
                    }
                }
            }, {
                title: "标签",
                data: "content.tags",
                orderable: false,
                width: "10%",
                render: function(tags) {
                    if (typeof(tags)=="undefined" || tags.length < 1){
                        return '';
                    }else {
                        var aCats = [];
                        $.each(tags, function (key, tag) {
                            aCats.push(tag['name']);
                        });
                        return aCats.join(',');
                    }
                }
            },{
                title: "语言",
                data: "content.vlangs",
                orderable: false,
                width: "5%",
                render: function(vlangs) {
                    var avlangs = [];
                    $.each(vlangs, function(key, vlang) {
                        avlangs.push(vlang['name']);
                    });
                    return avlangs.join(',');
                }
            },{
                title: "操作",
                orderable: false,
                width: "5%",
                "render": function(data, type, row, meta) {
                    return "<a class='btn btn-danger btn-xs' href='javascript:void(0);'" + "onclick='_deleteTagContent(\"" + row['id'] + "\")'>删除</a>";
                }
            }],
            // 分页类型
            "pagingType": "full_numbers",
            "sLoadingRecords": "正在加载数据...",
            "sZeroRecords": "暂无数据",
            // 能否进行搜索
            "searching": false,
            "order": [
                // [1, "asc"]
            ],
            "dom": 'rt<"bottom"iflp<"clear">>',
            "language": {
                "processing": "玩命加载中...",
                "lengthMenu": "显示 _MENU_ 项结果",
                "zeroRecords": "没有匹配结果",
                "info": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "infoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "infoFiltered": "(由 _MAX_ 项结果过滤)",
                "infoPostFix": "",
                "url": "",
                "paginate": {
                    "first": "首页",
                    "previous": "上一页",
                    "next": "下一页",
                    "last": "末页"
                }
            },
            _fnPageChange: function() {}
        });
        // 使用col插件实现表格头宽度拖拽
        $("#subcolumn-asset-selected-table").colResizable();
        // 全选
        $('#subcolumn-asset-selected-check-all').on('click', function() {
            $('#subcolumn-asset-selected-table tr input[type="checkbox"][name="checkbox-subcolumn-asset-selected"]').each(function() {
                if ($(this).is(':checked')) {
                    $(this).prop("checked", false);
                } else {
                    $(this).prop("checked", true);
                }
            });
        });
        // 批量删除媒资
        $('#batch-delete-subcolumn-asset').on('click', function() {
            var ids = [];
            $('#subcolumn-asset-selected-table tr input[type="checkbox"][name="checkbox-subcolumn-asset-selected"]').each(function() {
                if ($(this).is(':checked')) {
                    ids.push($(this).attr("data-id"));
                }
            });
            if (ids.length < 1) {
                layer.msg("媒资不能为空, 请勾选媒资!", {
                    icon: 5
                });
                return false;
            }
            layer.msg('确定要删除?', {
                time: 0, //不自动关闭,
                btn: ['确定', '取消'],
                yes: function(index) {
                    layer.close(index);
                    // 发送请求
                    $.ajax({
                        url: '/admin/entity/tag_content/delete',
                        type: 'POST',
                        cache: false,
                        dataType: 'json',
                        data: {
                            id: ids
                        },
                        success: function(data) {
                            if (data.status) {
                                layer.alert(data.content, {
                                    icon: 6
                                });
                            } else {
                                layer.msg(data.content, {
                                    icon: 5
                                });
                            }
                            subcolumnAssetSelectedTable.draw();
                        },
                        error: function() {
                            layer.msg("异常！");
                        }
                    });
                }
            });
        });
        //
        // 搜索过滤
        $('#subcolumn-asset-selected-search').on('click', function() {
            subcolumnAssetSelectedTable.draw();
        });
        // 刷新
        $('#subcolumn-asset-selected-refresh').on('click', function() {
            // subcolumnAssetSelectedTable.draw();
            subcolumnAssetSelectedTable.ajax.reload();
        });
    }

/**
 * [_deleteTagContent 删除标签-媒资关系]
 * @param  {[type]} id [description]
 * @return {[type]}    [description]
 */
    function _deleteTagContent(id) {
        layer.msg('确定要删除?', {
            time: 0, //不自动关闭,
            btn: ['确定', '取消'],
            yes: function(index) {
                layer.close(index);
                // 发送请求
                $.ajax({
                    url: '/admin/entity/tag_content/delete',
                    type: 'POST',
                    cache: false,
                    dataType: 'json',
                    data: {
                        id: id
                    },
                    success: function(data) {
                        if (data.status) {
                            layer.alert(data.content, {
                                icon: 6
                            });
                            subcolumnAssetSelectedTable.draw();
                        } else {
                            layer.msg(data.content, {
                                icon: 5
                            });
                        }
                    },
                    error: function() {
                        layer.msg("异常！");
                    }
                });
            }
        });
    }
</script>